#include "comm.h"
#include "sys/ipc.h"
#include "sys/shm.h"
#include "sys/types.h"
#include "unistd.h"


int main() {
    // 创建key
    key_t key = ftok(PATH_NAME, PROJ_ID);
    if (key < -1 ) {
        perror("ftok");
        return 1;
    }  
    
    // 如果不存在，创建；如果存在，则返回
   int shmid = shmget(key, SIZE, IPC_CREAT | IPC_EXCL | 0644);
    
   if (shmid < 0) {
       perror("shmget");
       return 1;
   } 
   printf("%d\n", shmid);
   
   sleep(5);
   // 将当前进程和共享内存进行关联
   char *shmaddr = (char*)shmat(shmid, NULL, 0);
   printf("server already attach on shared memory\n"); 

   // 开始通信
   //sleep(10);
   for ( ; ; ) {
       printf("%s\n", shmaddr);
       sleep(1);
   }

   // 当前进程和共享内存去关联
   shmdt(shmaddr);
   printf("server already dattch off shared menory\n");
    
   sleep(5);
   // 释放共享内存
   shmctl(shmid, IPC_RMID, NULL);
   printf("delete shared memory\n");
   

   return 0;
   

}
